using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DragonAwakening.Core.Graphics;
using Microsoft.Xna.Framework.Graphics;
using DragonAwakening.Core.Game;
using Microsoft.Xna.Framework;

namespace DragonAwakening.Graphics
{
    /// <summary>
    /// Windows Phone 7 view port.
    /// </summary>
    public class WinPhoneViewPort : IViewPort
    {
        /// <summary>
        /// Output canvas
        /// </summary>
        public SpriteBatch batch;

        /// <summary>
        /// Blank texture.
        /// </summary>
        private Texture2D blank;

        /// <summary>
        /// Initializes the view port.
        /// </summary>
        public WinPhoneViewPort(GraphicsDevice device, ITextureManager textureManager)
        {
            this.batch = new SpriteBatch(device);
            this.blank = textureManager.GetColorTexture(Color.White);
        }

        /// <summary>
        /// Draws the string
        /// </summary>
        public void DrawString(SpriteFont font, string text, Vector2 position, Color color)
        {
            this.batch.DrawString(font, text, position, color, 0.0F, Vector2.Zero, 1.0F, SpriteEffects.None, 0.9F);
        }

        /// <summary>
        /// Draws the texture.
        /// </summary>
        public void Draw(Texture2D texture, Vector2 position, Color color)
        {
            this.batch.Draw(texture, position, null, color, 0.0F, Vector2.Zero, 1.0F, SpriteEffects.None, 1.0F);
        }

        /// <summary>
        /// Draws the texture.
        /// </summary>
        public void Draw(Texture2D texture, Rectangle position, Color color)
        {
            this.batch.Draw(texture, position, null, color, 0.0F, Vector2.Zero, SpriteEffects.None, 1.0F);
        }

        /// <summary>
        /// Draws the texture.
        /// </summary>
        public void Draw(Texture2D texture, Vector2 position, Color color, float zBufferPosition)
        {
            this.batch.Draw(texture, position, null, color, 0.0F, Vector2.Zero, 1.0F, SpriteEffects.None, zBufferPosition);
        }

        /// <summary>
        /// Draws the texture.
        /// </summary>
        public void Draw(Texture2D texture, Rectangle position, Color color, float zBufferPosition)
        {
            this.batch.Draw(texture, position, null, color, 0.0F, Vector2.Zero, SpriteEffects.None, zBufferPosition);
        }

        /// <summary>
        /// Draws the line.
        /// </summary>
        public void DrawLine(Vector2 point1, Vector2 point2, Color color, float width)
        {
            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = Vector2.Distance(point1, point2);
            batch.Draw(this.blank, point1, null, color, angle, Vector2.Zero, new Vector2(length, width), SpriteEffects.None, 0.1F);
        }

        /// <summary>
        /// Draws the rectangle.
        /// </summary>
        public void DrawRect(Rectangle rect, Color color, float width)
        {
            this.DrawLine(new Vector2(rect.Left, rect.Top), new Vector2(rect.Right, rect.Top), color, width);
            this.DrawLine(new Vector2(rect.Right, rect.Top), new Vector2(rect.Right, rect.Bottom), color, width);
            this.DrawLine(new Vector2(rect.Right, rect.Bottom), new Vector2(rect.Left, rect.Bottom), color, width);
            this.DrawLine(new Vector2(rect.Left, rect.Bottom), new Vector2(rect.Left, rect.Top), color, width);
        }
    }
}
